package com.justech.eat.controller;

import com.justech.common.util.DateUtil;
import com.justech.eat.controller.req.OrderDefaultSetReq;
import com.justech.eat.controller.req.OrderDetailReq;
import com.justech.eat.entity.Order;
import com.justech.eat.entity.OrderDefaultSet;
import com.justech.eat.entity.OrderDetailAbnormalRecord;
import com.justech.eat.mapper.mysql.OrderDetailAbnormalRecordMapper;
import com.justech.eat.mapper.sqlserver.AccMonitorLogMapper;
import com.justech.eat.repository.AccMonitorLogRepository;
import com.justech.eat.service.UserOrientedOrderService;
import com.justech.eat.service.component.UserTokenService;
import com.justech.eat.util.DateUtilPlus;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @program: eat-service
 * @description: 订餐控制器
 * @author: feifei.li
 * @create: 2020-05-26 09:20
 **/
@RestController
@RequestMapping("userOrder")
public class OrderController {
    @Autowired
    AccMonitorLogMapper accMonitorLogMapper;
    @Autowired
    AccMonitorLogRepository accMonitorLogRepository;
    @Autowired
    UserOrientedOrderService userOrientedOrderService;
    @Autowired
    OrderDetailAbnormalRecordMapper orderDetailAbnormalRecordMapper;
    @Autowired
    UserTokenService userTokenService;

    /**
     * 本月报餐记录
     *
     * @return
     */
    @GetMapping(value = "thisMonthOrders")
    public List<Order> thisMonthOrders(HttpServletRequest request) {
        String jobNumber = userTokenService.getJobNumber(request);
        return userOrientedOrderService.thisMonthOrders(jobNumber);
    }

    @ApiOperation("获取用户某个月的报餐明细")
    @GetMapping("/{year}/{month}/orders")
    public List<Order> monthOrders(HttpServletRequest request, @PathVariable int month, @PathVariable int year) {
        String firstDayOfMonth = DateUtil.getFisrtDayOfMonth(year, month);
        String lastDayOfMonth = DateUtilPlus.getLastDayOfMonth(year, month);
        return userOrientedOrderService.getMonthOrders(firstDayOfMonth, lastDayOfMonth, userTokenService.getJobNumber(request));
    }

    @ApiOperation("获取用户某个月的报餐明细")
    @GetMapping("/orders")
    public List<Order> getMonthOrders(HttpServletRequest request, int month, int year) {
        String firstDayOfMonth = DateUtil.getFisrtDayOfMonth(year, month);
        String lastDayOfMonth = DateUtilPlus.getLastDayOfMonth(year, month);
        return userOrientedOrderService.getMonthOrders(firstDayOfMonth, lastDayOfMonth, userTokenService.getJobNumber(request));
    }

    /**
     * 上月报餐记录
     *
     * @return
     */
    @GetMapping(value = "previousMonthOrders")
    public List<Order> previousMonthOrders(HttpServletRequest request) {
        return userOrientedOrderService.previousMonthOrders(userTokenService.getJobNumber(request));
    }

    /**
     * 提交报餐信息
     *
     * @param orderDetailReq
     * @param request
     * @return
     */
    @PostMapping("book")
    public List<Order> book(@RequestBody @Valid OrderDetailReq orderDetailReq, HttpServletRequest request) {
        return userOrientedOrderService.book(orderDetailReq, orderDetailReq.getDate(), userTokenService.getJobNumber(request));
    }

    @PostMapping("defaultSet")
    public OrderDefaultSet defaultSet(@RequestBody @Valid OrderDefaultSetReq orderDefaultSetReq, HttpServletRequest request) {
        return userOrientedOrderService.defaultSet(orderDefaultSetReq, userTokenService.getJobNumber(request));
    }

    /**
     * 根据工号获取异常
     *
     * @return
     */
    @GetMapping("abnormal")
    public List<OrderDetailAbnormalRecord> getAbnormalByJobNumber(HttpServletRequest request) {
        return orderDetailAbnormalRecordMapper.findUnprocessedByJobNumber(userTokenService.getJobNumber(request));
    }
}
